<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02-闭包和面向对象</title>
</head>

<body>
    <script>
        //定义一个Car类

        class Car {
            constructor(color, weight) {
                this.kilo = 0 //行进距离
                this.color = color
                this.weight = weight
            }
            drive() {
                this.kilo++
                console.log(this.kilo);
            }
        }
        let car = new Car('red', 300)
        console.log(car);
        // car.drive()
        // car.drive()
        // car.drive()

        // 使用闭包,进行面向对象

        let getCar = function (color, weight) {
            let kilo = 0
            return {
                color,
                weight,
                drive() {
                    console.log(++kilo);
                }
            }
        }
        let car1 = getCar('blue', 400)
        console.log(car1);
        car1.drive()
        car1.drive()
        car1.drive()

        // ⭐每次调用函数getCar也能得到一个与car相关得对象，但闭包，只是普通得对象，没有类得概念
    </script>
</body>

</html>